#include<func.h>
#include"threadPool.h"
#include"head.h"
#include"taskQueue.h"
int makeWorker(threadPool_t *pThreadPool){
    for(int i =0;i < pThreadPool->threadNum;++i){
        pthread_create(pThreadPool->tidArr+1,NULL,threadFunc,(void*)pThreadPool);
    }
}

int OrderJudge(task_t * pTask){
    char buf[1024]={0};
    if(strncmp(pTask->buf,"cd ",3)==0){
        cd(pTask);
    }
    else if(strncmp(pTask->buf,"cd",2)==0){
        cd(pTask);
    }
    else if(strncmp(pTask->buf,"ls ",3)==0){
        ls(pTask);
    }
    else if(strncmp(pTask->buf,"ls",2)==0){
        ls(pTask);
    }
    else if(strncmp(pTask->buf,"puts ",5)==0){
       //cd(pTask);
       uploadFile(pTask);
    }
    else if(strncmp(pTask->buf,"gets ",5)==0){
        //puts("gets");
        downloadFile(pTask);
    }
    else if(strncmp(pTask->buf,"remove ",7)==0){
        //puts("remove");
        removeFile(pTask);
    }
    else if(strncmp(pTask->buf,"pwd ",4)==0){
       printWorkingDir(pTask);
       //puts("pwd");
    }
    else if(strncmp(pTask->buf,"mkdir ",6)==0){
        myMkdir(pTask);
    }
    else if(strncmp(pTask->buf,"rmdir ",6)==0){
        //removeFile(pTask);
        puts("rmdir");
    }
    else{
        send(pTask->netFd,"命令错误，重新输入！",30,0);
        return -1;
    }
}
void *threadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg; //地址传递传入传出参数
    while (1){
        task_t *pTask;//新建一个task来接收队首的任务
        pthread_mutex_lock(&pThreadPool->mutex);
        while (pThreadPool->taskQueue.size == 0){
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);//解锁了等一个信号
        }
        puts("GetTask");//来信号了自动上锁
        pTask = pThreadPool->taskQueue.pFront;
        deQueue(&pThreadPool->taskQueue);
        pthread_mutex_unlock(&pThreadPool->mutex);
        printf("netFd = %d\n",pTask->netFd);
        handleEvent(pTask);
    }
}
int handleEvent(task_t* pTask){
    task_t mypTask = *pTask; //接收下来
    puts("welcome into handEvent!");
    int netFd = mypTask.netFd;
    //usrConnectRecord(mypTask);//记录日志信息cat /var/log/syslog查看日志信息
    //从这开始是密码验证需要的，将
    while(1){
        char userName[20] = {0};
        char password[20] = {0};
        int ret = recv(netFd, userName, sizeof(userName), 0);
        ERROR_CHECK(ret,-1,"recv1");
        ret = recv(netFd, password, sizeof(password), 0);
        ERROR_CHECK(ret,-1,"recv2");
        //puts("1");
        ret = checkUserPass(userName, password);
        //puts("2");
        if(ret == 0){
            send(netFd, "0", 2, 0);
            break;
        }else if(ret == -1){
            send(netFd, "-1", 3, 0);
            //循环再次输入
        }
    }
    usrConnectRecord(mypTask);//记录登录时间
    while (1){
        bzero(mypTask.buf,sizeof(mypTask.buf));
        int ret = recv(mypTask.netFd,mypTask.buf,sizeof(mypTask.buf),0);
        if(ret == 0){
            puts("对面客户端断开！");
            break;
        }
        usrCommandRecord(mypTask);
        OrderJudge(&mypTask);
    }
    close(netFd);
    return 0;
}
